﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Zr
{
    public partial class StuInfoMange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
                Bind2();
            }
        }

        private void Bind()
        {

            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.stuid,a.stuname,a.sex,a.age,a.mz,a.syd,a.birthtime,a.rxrq,b.classname from Student a,class b where a.classid=b.classid and b.schoolid=(select schoolid from zhuren where zhurenid='" + Session["zhurenid"].ToString() + "')";//只能管该系的学生
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

        private void Bind2()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.schoolname from School a,Zhuren b where zhurenID='" + Session["zhurenid"].ToString() + "' and a.schoolid=b.schoolid";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            Label1.Text = ds.Tables[0].Rows[0][0].ToString() + "的成绩列表";//标题

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string xiu = e.CommandArgument.ToString();//获取这个按钮的操作值
            if (e.CommandName == "xiu")
            {
                //传递修改的标志 跳转页面
                Response.Redirect("stuInfodetails.aspx?Flag=xiu&ID=" + xiu);
            }
            else
            {
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "select * from Score where stuid='" + xiu + "'";//判断成绩表里有没有记录
                string sql2 = "select * from Fee where stuid='" + xiu + "'";//判断费用表有没有记录
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlCommand cmd2 = new SqlCommand(sql2, conn);
                object a = cmd.ExecuteScalar();
                object a2 = cmd2.ExecuteScalar();
                if (a != null)
                {

                    if (a2 != null)
                    {
                        Label2.Text = "<font color='red'>成绩表和费用表都有该学生信息记录,不能删除</font>";

                    }
                    else
                    {
                        Label2.Text = "<font color='red'>成绩表有该学生信息记录,不能删除</font>";
                    }

                }
                else
                {
                    if (a2 != null)
                    {
                        Label2.Text = "<font color='red'>费用表有该学生信息记录,不能删除</font>";
                    }
                    else
                    {
                        //删除
                        string sql3 = "delete from student where stuid='" + xiu + "'";//删除这条学生记录
                        SqlCommand cmd3 = new SqlCommand(sql3, conn);
                        int a3 = cmd3.ExecuteNonQuery();
                        if (a3 > 0)
                        {
                            this.Page.RegisterStartupScript(" ", "<script>alert('删除成功'); </script> ");
                            Bind();
                            Label2.Text = "";
                        }
                        else
                        {
                            this.Page.RegisterStartupScript(" ", "<script>alert('删除失败 '); </script> ");
                        }
                    }
                }
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            //跳转页面 
            Response.Redirect("stuInfodetails.aspx?Flag=Add");
        }
    }
}